// DriveNotes. Copyright (c) 2011, Andrei Senchuk. All rights reserved.
package net.taviscaron.drivenotes.web.action;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.taviscaron.drivenotes.shared.model.entity.Car;
import net.taviscaron.drivenotes.shared.model.entity.User;
import net.taviscaron.drivenotes.shared.service.GarageService;
import net.taviscaron.drivenotes.web.core.Constants;
import net.taviscaron.drivenotes.web.forward.RequestForwarder;
import net.taviscaron.drivenotes.web.forward.ViewForward;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Required;

/**
 * Garage action
 * @author Andrei Senchuk
 */
public class GarageAction implements Action {
    private static final Log logger = LogFactory.getLog(GarageAction.class);
    private GarageService garageService;

    @Required
    public void setGarageService(GarageService garageService) {
        this.garageService = garageService;
    }

    public RequestForwarder doAction(HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getAttribute(Constants.AUTHORIZED_USER_ATTR_NAME);
        List<Car> cars = garageService.findCarsByOwnerId(user.getId());
        request.setAttribute("cars", cars);
        return new ViewForward("garage");
    }
}
